package com.itgaohe.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itgaohe.common.context.UserContext;
import com.itgaohe.dao.entity.Like;
import com.itgaohe.dao.mapper.LikeMapper;
import com.itgaohe.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import static com.itgaohe.common.constant.RedisConstants.CACHE_LIKE_KEY;

@Service
public class LikeServiceImpl extends ServiceImpl<LikeMapper, Like> implements LikeService {
    @Autowired
    private LikeMapper likeMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 判断用户是否点赞
     * @param blogId
     * @return TRUE:已点赞 FALSE:未点赞
     */
    @Override
    public Boolean getIsLike(Long blogId) {
        Long currentUserId = UserContext.getUser().getId();
        String key = CACHE_LIKE_KEY + blogId+currentUserId;
        String s = stringRedisTemplate.opsForValue().get(key);
        if (s != null){
            return true;
        }
        if (likeMapper.getIsLike(blogId,currentUserId) != null){
            return true;
        }
        return true;
    }
}
